

package com.scalar.chiptrack.db;

import java.io.File;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.Attributes;

import com.scalar.chiptrack.utils.ConfigManager;
import com.scalar.chiptrack.commons.Constants;
import com.scalar.ScalarException;

/**
 *
 * @author Balaram Putta
 * @version
 */
public final class DataSourceConfig extends DefaultHandler
{

    private static final String MAX_CONN = "maxConn";
    private static final String AUTO_COMMIT = "autoCommit";
    private static final String DRIVER_CLASS = "driverclass";
    private static final String URL = "url";
    private static final String USER_NAME = "userName";
    private static final String PASSWORD = "password";
    
    private String m_configFile = null;
    private StringBuffer m_buf = null;
    
    private String m_driverClass = null;
    private String m_url = null;
    private String m_userName = null;
    private String m_password = null;
    private boolean m_autoCommit = false;
    private int m_maxConn = 0;


    public DataSourceConfig() throws ScalarException {
        
        m_configFile = ConfigManager.getDataSourceConfigFile();
        m_buf = new StringBuffer();
        
        if (isValidFile (m_configFile) == false) {
            throw new ScalarException ("No/Invalid config file specified for Datasource configuration");
        }
        
        try {
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser parser = factory.newSAXParser();
            parser.parse (m_configFile, this);
        }
        catch (Exception e) {
            e.printStackTrace();
            throw new ScalarException ("Exception while reading DataSource config file", e);
        }
    }


    /**
     * Loads database configuration bsaed on the datasource type passed.
     *
     * @param datasourceType one of GREAT_PLAINS_DATASOURCE or LOCAL_DATASOURCE
     * @throws ScalarException
     */
    public DataSourceConfig( int datasourceType ) throws ScalarException
    {
        if( datasourceType == Constants.DATASOURCE_TYPE_GREAT_PLAINS )
        {
            m_configFile = ConfigManager.getGreatPlainsDataSourceConfigFile();
        }
        else if( datasourceType == Constants.DATASOURCE_TYPE_MSACCESS )
        {
            m_configFile = ConfigManager.getMsAcessDataSourceConfigFile();
        }
        else
        {
            m_configFile = ConfigManager.getDataSourceConfigFile();
        }

        m_buf = new StringBuffer();

        if( isValidFile( m_configFile ) == false )
        {
            throw new ScalarException( "No/Invalid config file specified for Datasource configuration" );
        }

        try
        {
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser parser = factory.newSAXParser();
            parser.parse (m_configFile, this);
        }
        catch( Exception e )
        {
            e.printStackTrace();
            throw new ScalarException( "Exception while reading DataSource config file", e );
        }
    }



    public String getUserName() {
        return m_userName;
    }
    
    public String getPassword() {
        return m_password;
    }
    
    public String getUrl() {
        return m_url;
    }
    
    public String getDriverClass() {
        return m_driverClass;
    }
    
    public boolean getAutoCommit() {
        return m_autoCommit;
    }
    
    public int getMaxConn() {
        return m_maxConn;
    }
    
    
    
    
    
    // PARSE THE XML FILE 
    
    public void startDocument() {
    }
    
    public void endDocument() {
    }
    
    public void startElement (String uri, String localName, String qname, Attributes attrs) {
    }
    
    public void endElement(String uri, String localName, String qname) {

        if (qname.equalsIgnoreCase (MAX_CONN) == true) {
            m_maxConn = Integer.parseInt(m_buf.toString().trim());
        }       
        else if (qname.equalsIgnoreCase (AUTO_COMMIT) == true) {
            m_autoCommit = new Boolean (m_buf.toString().trim()).booleanValue();
        }
        else if (qname.equalsIgnoreCase (USER_NAME) == true) {
            m_userName = m_buf.toString().trim();
        }
        else if (qname.equalsIgnoreCase (PASSWORD) == true) {
            m_password = m_buf.toString().trim();
        }
        else if (qname.equalsIgnoreCase (DRIVER_CLASS) == true) {
            m_driverClass = m_buf.toString().trim();
        }
        else if (qname.equalsIgnoreCase (URL) == true) {
            m_url = m_buf.toString().trim();
        }
        m_buf.delete(0, m_buf.length());
    }
    
    public void characters(char[] ch, int start, int length) {
        m_buf.append (ch, start, length);
    }
    
    private boolean isValidFile (String fileName) {

        if (fileName == null) {
            return false;
        }
        
        File file = new File (fileName);
        
        if (file.exists() == false) {
            return false;
        }
        if (file.isFile() == false) {
            return false;
        }
        if (file.canRead() == false) {
            return false;
        }
        
        return true;
    }
    
    
    public static void main (String[] args) throws ScalarException {
        
        DataSourceConfig config = new DataSourceConfig();
        
        System.out.println ("url = " + config.getUrl());
        System.out.println ("driver class = " + config.getDriverClass());
        System.out.println ("user name = " + config.getUserName());
        System.out.println ("password = " + config.getPassword());
        System.out.println ("max conn = " + config.getMaxConn());
        System.out.println ("auto commit = " + config.getAutoCommit());
    }
}